package com.shaw.workrecord.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shaw.workrecord.dto.Children;
import com.shaw.workrecord.dto.Record;
import com.shaw.workrecord.exception.CustomerException;
import com.shaw.workrecord.mapper.ChildrenMapper;
import com.shaw.workrecord.pojo.request.ChildrenAddRequest;
import com.shaw.workrecord.pojo.request.ChildrenEditRequest;
import com.shaw.workrecord.pojo.response.ChildrenResponse;
import com.shaw.workrecord.pojo.response.RecordResponse;
import com.shaw.workrecord.service.IChildrenService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author jiangjunhui
 * @description
 * @date 2023/8/9 17:51
 */
@Service
public class ChildrenServiceImpl extends ServiceImpl<ChildrenMapper, Children> implements IChildrenService {

    @Override
    public List<ChildrenResponse> getChildrenList() {
        LambdaQueryWrapper<Children> wrapper = new LambdaQueryWrapper<>();
        wrapper.select().orderByAsc(Children::getCreateTime);
        List<Children> result = baseMapper.selectList(wrapper);
        return result.stream().map(i -> {
            return BeanUtil.copyProperties(i, ChildrenResponse.class);
        }).collect(Collectors.toList());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addChildren(ChildrenAddRequest request) {
        Children children = BeanUtil.copyProperties(request, Children.class);
        return save(children);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean editChildren(ChildrenEditRequest request) {
        Children children = BeanUtil.copyProperties(request, Children.class);
        return saveOrUpdate(children);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeChildren(Long id) {
        LambdaQueryWrapper<Children> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(Children::getId).eq(Children::getId, id);
        Children children = baseMapper.selectOne(wrapper);
        if (children == null) {
            throw new CustomerException("不存在该数据");
        }
        return removeById(id);
    }
}
